package com.loong.ems.service.impl;

import com.loong.ems.domain.entity.EmpLeaveRecord;
import com.loong.ems.domain.vo.resp.EmpLeaveRecordVo;
import com.loong.ems.mapper.EmpLeaveRecordMapper;
import com.loong.ems.service.EmpLeaveRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 龙勇-229970722
 * @date 2025-01-03 14:39
 * @description: 请假服务接口实现
 */

@Service
public class EmpLeaveRecordServiceImpl implements EmpLeaveRecordService {
    @Autowired
    private EmpLeaveRecordMapper empLeaveRecordMapper;

    /**
     * 获取请假记录列表
     * @return
     */
    @Override
    public List<EmpLeaveRecordVo> getLeaveRecordList() {
        return empLeaveRecordMapper.getLeaveRecordList();
    }

    /**
     * 获取待审批请假记录列表
     * @return
     */
    @Override
    public List<EmpLeaveRecordVo> getPendingLeaveRecordList() {
        return empLeaveRecordMapper.getPendingLeaveRecordList(1);
    }

    /**
     * 获取待销假请假记录列表
     * @return
     */
    @Override
    public List<EmpLeaveRecordVo> getPendingLeaveCancelRecordList() {
        return empLeaveRecordMapper.getPendingLeaveRecordList(4);
    }

    /**
     * 添加请假记录
     * @param empLeaveRecord
     * @return
     */
    @Override
    public int addLeaveRecord(EmpLeaveRecord empLeaveRecord) {
        // 校验开始时间是否晚于结束时间
        if (empLeaveRecord.getLeaveStartTime() != null && empLeaveRecord.getLeaveEndTime() != null) {
            if (empLeaveRecord.getLeaveStartTime().after(empLeaveRecord.getLeaveEndTime())) {
                return -1;
            }
        }
        return empLeaveRecordMapper.insert(empLeaveRecord);
    }

    /**
     * 删除请假记录
     * @param id
     * @return
     */
    @Override
    public int deleteLeaveRecord(Long id) {
        return empLeaveRecordMapper.deleteById(id);
    }

    /**
     * 获取请假记录数量
     * @return
     */
    @Override
    public int countLeave() {
        return empLeaveRecordMapper.getCount();
    }

    /**
     * 获取某个员工的请假记录列表
     * @param userId
     * @return
     */
    @Override
    public List<EmpLeaveRecordVo> personLeave(Long userId) {
        return empLeaveRecordMapper.getLeaveRecordListByUserId(userId);
    }

    /**
     * 更新请假记录
     * @param empLeaveRecord
     * @return
     */
    @Override
    public int updateLeaveRecord(EmpLeaveRecord empLeaveRecord) {
        // 校验开始时间是否晚于结束时间
        if (empLeaveRecord.getLeaveStartTime() != null && empLeaveRecord.getLeaveEndTime() != null) {
            if (empLeaveRecord.getLeaveStartTime().after(empLeaveRecord.getLeaveEndTime())) {
                return -1;
            }
        }
        // 更新记录
        return empLeaveRecordMapper.updateByPrimaryKeySelective(empLeaveRecord);
    }

}
